package packetizer

import (
	"sync"

	"github.com/pion/rtp"
)

// NewFixedSequencer returns a new sequencer starting from a specific
// sequence number
func NewFixedSequencer(s uint16) rtp.Sequencer {
	return &sequencer{
		sequenceNumber: s,
	}
}

type sequencer struct {
	sequenceNumber uint16
	rollOverCount  uint64
	mutex          sync.Mutex
}

// NextSequenceNumber increment and returns a new sequence number for
// building RTP packets
func (s *sequencer) NextSequenceNumber() uint16 {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	seq := s.sequenceNumber
	s.sequenceNumber++

	return seq
}

// RollOverCount returns the amount of times the 16bit sequence number
// has wrapped
func (s *sequencer) RollOverCount() uint64 {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	return s.rollOverCount
}
